import dt from '@dt/dt'
import templateMgr from '@/biz/bonus/template.js'

let _this = null
let positionList = []
getPosition()

function getPosition() {
  templateMgr.bonusPosition().then((res) => {
    positionList = res
  })
}

function positions(list, row) {
  let data = [...positionList]
  let index = list.findIndex((i) => i == row)
  list.forEach((item, idx) => {
    if (idx !== index) {
      for (let it of item.postIds) {
        data = data.filter((i) => i.key != it)
      }
    }
  })
  return data
}

function settlementRatio(row) {
  if (_this.info.templateType == 'PRE') {
    let options = dt.dictInfo().bonusSettlementTypeTwo
    // 编辑页面 处理结算条件及比例选项
    if (_this.$route.name == 'bonusTemplateEdit') {
      let org = dt.buildInfo().caseLocationTwo
      options =
        org == '上海二公司' || org == '上海三公司' || org == '上海四公司' || org == '上海五公司'
          ? options
          : options.filter((i) => i.key != 'CONTRACT_SELF')
    }
    return [
      {
        key: 'settlementType',
        disabled: _this.$route.name !== 'bonusTemplateEdit' ? true : false,
        select: { options }
      },
      {
        key: 'settlementProportion',
        placeholder: '请输入比例',
        disabled: _this.$route.name !== 'bonusTemplateEdit' ? true : false,
        input: {
          type: 'number',
          min: 0,
          max: 100
        }
      }
    ]
  } else {
    let data = [],
      condition = dt.dictInfo().bonusSettlementType
    for (let item of condition) {
      data.push([
        {
          placeholder: item.value,
          col: 4,
          text: {}
        },
        {
          key: item.key,
          col: 20,
          placeholder: '请输入比例',
          disabled: _this.$route.name !== 'bonusTemplateEdit' ? true : false,
          input: {
            type: 'number',
            min: 0,
            max: 100
          }
        }
      ])
    }
    return data
  }
}

function handles() {
  return _this.$route.name === 'bonusTemplateEdit'
    ? [
        {
          field: 'handles',
          title: '操作',
          width: 70,
          cell: (row) => {
            return {
              btn: [
                {
                  title: '删除',
                  event: 'onDelete'
                }
              ]
            }
          }
        }
      ]
    : []
}

function options(vm) {
  _this = vm
  return [
    {
      type: 'index',
      title: '序号',
      width: 70
    },
    {
      title: '名称',
      minWidth: 130,
      cell: {
        edit: {
          key: 'detailName',
          placeholder: '请输入名称',
          disabled: _this.$route.name !== 'bonusTemplateEdit' ? true : false,
          input: {}
        }
      }
    },
    {
      title: '涉及岗位',
      minWidth: 180,
      cell: (row) => {
        let options = positions(_this.list, row)
        if (_this.$route.name === 'bonusTemplateEdit') {
          return {
            edit: {
              key: 'postIds',
              select: {
                options,
                multiple: true,
                maxTagCount: 1
              }
            }
          }
        } else {
          let data = options.filter((i) => {
            let data = row.postIds.find((o) => o == i.key)
            if (data) return data
          })
          return {
            value: () => {
              return data.map((i) => i.value).join(',')
            },
            text: {
              whiteSpace: 'pre-wrap'
            }
          }
        }
      }
    },
    {
      title: '发放标准',
      minWidth: 180,
      cell: (row) => {
        let arr = [
          {
            key: 'issueType',
            disabled: _this.$route.name !== 'bonusTemplateEdit' ? true : false,
            select: {
              options: dt.dictInfo().issueType
            }
          }
        ]
        if (row.issueType == '1') {
          arr.push({
            key: 'scaleType',
            disabled: _this.$route.name !== 'bonusTemplateEdit' ? true : false,
            select: {
              options: _this.info.templateType == 'PRE' ? dt.dictInfo().scaleTypeTwo : dt.dictInfo().scaleType
            }
          })
        } else {
          arr.push({
            key: 'fixedAmount',
            placeholder: '请输入固定金额',
            disabled: _this.$route.name !== 'bonusTemplateEdit' ? true : false,
            input: {
              type: 'number',
              min: 0
            }
          })
        }
        return {
          group: arr
        }
      }
    },
    {
      title: '结算条件及比例(%)',
      width: 260,
      cell: (row) => {
        return {
          group: settlementRatio(row)
        }
      }
    },
    ...handles()
  ]
}

export default {
  options
}
